package cn.topcodes.tcsf.wechat.listener;

import cn.topcode.unicorn.wxsdk.WXMessageListener;
import cn.topcode.unicorn.wxsdk.message.receive.msg.ShortVideoMessage;
import cn.topcodes.tcsf.wechat.domain.entity.WechatMessage;
import cn.topcodes.tcsf.wechat.domain.entity.WechatMp;
import cn.topcodes.tcsf.wechat.service.WechatMessageServiceImpl;
import cn.topcodes.tcsf.wechat.service.WechatMpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

/**
 * Created by Unicorn on 2017/8/17.
 */
@Component
@Lazy(false)
public class ShortVideoMessageListener extends WXMessageListener<ShortVideoMessage> {

    @Autowired
    private WechatMpService wechatMpService;

    @Autowired
    private WechatMessageServiceImpl wechatMessageService;

    @Override
    public void onReceive(ShortVideoMessage message) {
        WechatMessage msg = new WechatMessage();
        msg.setToUserName(message.getToUserName());
        WechatMp wechatMp = wechatMpService.findWechatMpByMpId(message.getToUserName());
        msg.setWechatMpId(wechatMp.getId());
        msg.setFromUserName(message.getFromUserName());
        msg.setTime(message.getCreateTime());
        msg.setMsgType(message.getMsgType());
        msg.setMsgId(message.getMsgId());

        msg.setMediaId(message.getMediaId());
        msg.setThumbMediaId(message.getThumbMediaId());
        msg.setType(WechatMessage.TYPE_RECEIVE);

        this.wechatMessageService.create(msg);
    }
}
